package com.roc.steelend.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.roc.steelend.controller.vo.returnformat.ReturnMsg;
import com.roc.steelend.controller.vo.tiaopiao.FindTiaoPiaoReception;
import com.roc.steelend.controller.vo.tiaopiao.TiaoPiaoKaipingReception;
import com.roc.steelend.controller.vo.tiaopiao.TiaoPiaoSaleReception;
import com.roc.steelend.mapper.KaipingGuyMapper;
import com.roc.steelend.mapper.KaipingOrderMapper;
import com.roc.steelend.mapper.TiaoPiaoKaipingMapper;
import com.roc.steelend.pojo.KaipingGuy;
import com.roc.steelend.pojo.TiaoPiaoKaiping;
import com.roc.steelend.service.TiaoPiaoKaipingService;
import com.roc.steelend.service.TiaoPiaoSaleService;
import com.roc.steelend.util.LshUtil;
import javafx.util.converter.TimeStringConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Service
@Transactional(rollbackFor = Exception.class)
public class TiaoPiaoKaipingServiceImpl  implements TiaoPiaoKaipingService {

    @Autowired
    private TiaoPiaoKaipingMapper tiaoPiaoKaipingMapper;
    @Autowired
    private KaipingGuyMapper kaipingGuyMapper;
    @Autowired
    private KaipingOrderMapper kaipingOrderMapper;
    @Override
    public ReturnMsg postTiaoPiaoByKaiping(TiaoPiaoKaipingReception tiaoPiaoKaipingReception, Integer employeeId) {
        ReturnMsg msg = new ReturnMsg();
        TiaoPiaoKaiping tiaoPiaoKaiping = new TiaoPiaoKaiping();
        BigInteger tiaoPiaoKaipingLsh = LshUtil.getTiaoPiaoKaiping();
        tiaoPiaoKaiping.setTiaoPiaoKaipingId(tiaoPiaoKaipingLsh);
        tiaoPiaoKaiping.setEmployeeId(employeeId);
        tiaoPiaoKaiping.setKaipingguyId(tiaoPiaoKaipingReception.getKaipingguyId());
        tiaoPiaoKaiping.setOperateTime(new Timestamp(new Date().getTime()));
        tiaoPiaoKaiping.setTiaoPiaoKaipingTime(tiaoPiaoKaipingReception.getTiaoPiaoTime());
        tiaoPiaoKaiping.setOriginallyInvoice(tiaoPiaoKaipingReception.getOldInvoice());
        tiaoPiaoKaiping.setNowInvoice(tiaoPiaoKaipingReception.getNewInvoice());
        if(Objects.equals(tiaoPiaoKaipingReception.getRemark(), "")){
            tiaoPiaoKaipingReception.setRemark(null);
        }
        tiaoPiaoKaiping.setRemark(tiaoPiaoKaipingReception.getRemark());
        int insert = tiaoPiaoKaipingMapper.insert(tiaoPiaoKaiping);
        if (insert!=1){
            msg.setCode(0);
            msg.setMsg("调票操作失败，请重试");
            return  msg;
        }

        QueryWrapper<KaipingGuy> wrapper = new QueryWrapper<>();

        wrapper.eq("kaipingguy_id",tiaoPiaoKaipingReception.getKaipingguyId());
        KaipingGuy kaipingGuy = kaipingGuyMapper.selectList(wrapper).get(0);
        kaipingGuy.setInvoice(tiaoPiaoKaipingReception.getNewInvoice());
        kaipingGuyMapper.updateById(kaipingGuy);


        kaipingOrderMapper.updateTiaoPiaoByKaipingGuy(tiaoPiaoKaiping.getKaipingguyId(), tiaoPiaoKaipingLsh);

        msg.setMsg("调票操作成功");
        msg.setCode(1);
        return msg;
    }

    @Override
    public List<HashMap<String, Object>> findTiaoPiaoDataByKaiping(FindTiaoPiaoReception findTiaoPiaoReception) {
        Integer now = findTiaoPiaoReception.getCurrentPage();
        Integer size = findTiaoPiaoReception.getPageSize();
        now=(now-1)*size;
        String duiXiang = findTiaoPiaoReception.getDuiXiang();
        if (Objects.equals(duiXiang, "")||duiXiang==null){
            duiXiang=null;
        }else {
            duiXiang="%"+duiXiang+"%";
        }
        return tiaoPiaoKaipingMapper.findTiaoPiaoDataByKaiping(now,size,duiXiang);
    }

    @Override
    public Integer findTiaoPiaoDataByKaipingCount(FindTiaoPiaoReception findTiaoPiaoReception) {
        String duiXiang = findTiaoPiaoReception.getDuiXiang();
        if (Objects.equals(duiXiang, "")||duiXiang==null){
            duiXiang=null;
        }else {
            duiXiang="%"+duiXiang+"%";
        }
        return tiaoPiaoKaipingMapper.findTiaoPiaoDataByKaipingCount(duiXiang);
    }

    @Override
    public List<TiaoPiaoKaiping> kaipingTiaoPiaoDetailSelect(BigInteger tiaoPiaoId) {

        QueryWrapper<TiaoPiaoKaiping> tiaoPiaoKaipingQueryWrapper = new QueryWrapper<>();
        tiaoPiaoKaipingQueryWrapper.eq("tiao_piao_kaiping_id",tiaoPiaoId);

        return tiaoPiaoKaipingMapper.selectList(tiaoPiaoKaipingQueryWrapper);
    }
}
